package cn.com.checne.controller.superManager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.constants.LogConstants;
import cn.com.checne.constants.NavigateConstants;
import cn.com.checne.controller.BaseController;
import cn.com.checne.dao.LogInfoDao;
import cn.com.checne.dao.UserInfoDao;
import cn.com.checne.model.domain.RightInfo;
import cn.com.checne.model.domain.RoleInfo;
import cn.com.checne.model.domain.UserInfo;
import cn.com.checne.util.PageBean;
import cn.com.checne.util.SecurityMD5;
import cn.com.checne.util.WebUtils;

@Controller
@RequestMapping(value="/super/user")
public class UserInfoManagerController extends BaseController {
	protected static final Logger logger = Logger.getLogger(UserInfoManagerController.class);

	@Resource
	private LogInfoDao logDao;
	@Resource
	private UserInfoDao userDao;
	
	//系统用户--首页
	@RequestMapping(value="/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/frame/frame_user";
	}
	
	//系统用户--左侧导航
	@RequestMapping(value="/left.do")
	public String left(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/user/left";
	}
	
	//系统用户--用户-列举所有
	@RequestMapping(value="/user-list.do")
	public ModelAndView userList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		List<RoleInfo> roleList = userDao.getRoleList(0,Integer.MAX_VALUE);

		int roleId = WebUtils.getRequestParameterAsInt(request, "roleId", 0);
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<UserInfo> userList = userDao.getUserListByRoleId(roleId, pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(userDao.getUserCountByRoleId(roleId));
		pageBean.setMaxPage();
		pageBean.setPageNoList();
		
		modelMap.put("roleList",roleList);
		modelMap.put("roleId", roleId);
		modelMap.put("userList", userList);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/user/user-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--用户--新增、修改页面
	@RequestMapping(value="/user-show.do")
	public ModelAndView userShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		List<RoleInfo> roleList = userDao.getRoleList(0,Integer.MAX_VALUE);
		int userId = WebUtils.getRequestParameterAsInt(request, "userId", 0);
		int roleId = WebUtils.getRequestParameterAsInt(request, "roleId", 0);
		UserInfo user = new UserInfo();
		String opt = "create" ;
		if(userId > 0){
			user = userDao.getUser(userId);
			opt = "update" ;
		}
		modelMap.put("roleList",roleList);
		modelMap.put("user", user);
		modelMap.put("roleId", roleId);
		modelMap.put("opt", opt);
		mav.setViewName("super/user/user-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--用户--新增
	@RequestMapping(value="/user-create.do")
	public ModelAndView userCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		UserInfo user = new UserInfo();
		user.setRoleId(WebUtils.getRequestParameterAsInt(request, "roleId",1));
		user.setUsername(WebUtils.getRequestParameterAsString(request, "username"));
		String password = WebUtils.getRequestParameterAsString(request, "password");
		String passwordComfrim = WebUtils.getRequestParameterAsString(request, "passwordComfrim");
		if(!StringUtils.equals(password, passwordComfrim)){
			message = "两次密码输入不一致!" ;
			modelMap.put("message", message);
			mav.setViewName("common/message");
			return mav ;
		}
		password = SecurityMD5.crypt(password);
		user.setPassword(password);
		user.setRealName(WebUtils.getRequestParameterAsString(request, "realName"));
		user.setDeptName(WebUtils.getRequestParameterAsString(request, "deptName"));
		user.setTelphone(WebUtils.getRequestParameterAsString(request, "telphone"));
		user.setMobilephone(WebUtils.getRequestParameterAsString(request, "mobilephone"));
		user.setEmail(WebUtils.getRequestParameterAsString(request, "email"));
		if(userDao.createUser(user)){
			message = "系统用户创建成功!" ;
			UserInfo u = (UserInfo) request.getSession().getAttribute(Constants.SESSIONNAME);
			logDao.create(u.getId(), LogConstants.ACTION_INSERT, NavigateConstants.NAVI_USER, "创建用户");
		}else{
			message = "系统用户创建失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/user/user-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--用户--修改
	@RequestMapping(value="/user-update.do")
	public ModelAndView userUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		int userId = WebUtils.getRequestParameterAsInt(request, "userId",0);
		UserInfo user = new UserInfo();
		if(userId > 0){
			user = userDao.getUser(userId);
			user.setRoleId(WebUtils.getRequestParameterAsInt(request, "roleId",1));
			user.setUsername(WebUtils.getRequestParameterAsString(request, "username"));
			String password = WebUtils.getRequestParameterAsString(request, "password");
			String passwordComfrim = WebUtils.getRequestParameterAsString(request, "passwordComfrim");
			if(StringUtils.isBlank(password)){
				password = user.getPassword();
			}else{
				if(!StringUtils.equals(password, passwordComfrim)){
					message = "两次密码输入不一致!" ;
					modelMap.put("message", message);
					mav.setViewName("common/message");
					return mav ;
				}else{
					password = SecurityMD5.crypt(password);
				}
			}
			user.setPassword(password);
			user.setRealName(WebUtils.getRequestParameterAsString(request, "realName"));
			user.setDeptName(WebUtils.getRequestParameterAsString(request, "deptName"));
			user.setTelphone(WebUtils.getRequestParameterAsString(request, "telphone"));
			user.setMobilephone(WebUtils.getRequestParameterAsString(request, "mobilephone"));
			user.setEmail(WebUtils.getRequestParameterAsString(request, "email"));
			if(userDao.updateUser(user)){
				message = "系统用户修改成功!" ;
				logDao.create(user.getId(), LogConstants.ACTION_MODIFY, NavigateConstants.NAVI_USER, "修改用户 " + user.getUsername());
			}else{
				message = "系统用户修改失败!" ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/user/user-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--用户--重置
	@RequestMapping(value="/user-reset.do")
	public ModelAndView userReset(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		int userId = WebUtils.getRequestParameterAsInt(request, "userId",0);
		UserInfo user = new UserInfo();
		if(userId > 0){
			user = userDao.getUser(userId);
			user.setPassword(SecurityMD5.crypt("admin"));
			if(userDao.updateUser(user)){
				message = "系统用户密码重置修改成功!新密码为<font color='red'>admin</font>" ;
				logDao.create(user.getId(), LogConstants.ACTION_MODIFY, NavigateConstants.NAVI_USER, "重置用户" + user.getUsername() + "密码 ");
			}else{
				message = "系统用户密码重置修改失败!" ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/user/user-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--角色-列举所有
	@RequestMapping(value="/role-list.do")
	public ModelAndView roleList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<RoleInfo> roleList = userDao.getRoleList(0,Integer.MAX_VALUE);
		pageBean.setMaxRowCount(userDao.getRoleCount());
		pageBean.setMaxPage();
		pageBean.setPageNoList();
		
		modelMap.put("roleList",roleList);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/user/role-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--角色--新增、修改页面
	@RequestMapping(value="/role-show.do")
	public ModelAndView roleShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int roleId = WebUtils.getRequestParameterAsInt(request, "roleId", 0);
		RoleInfo role = new RoleInfo();
		String opt = "create" ;
		if(roleId > 0){
			role = userDao.getRoleInfoByRoleId(roleId);
			opt = "update" ;
		}
		modelMap.put("role", role);
		modelMap.put("opt", opt);
		mav.setViewName("super/user/role-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--角色--新增
	@RequestMapping(value="/role-create.do")
	public ModelAndView roleCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		RoleInfo role = new RoleInfo();
		role.setRoleName(WebUtils.getRequestParameterAsString(request, "roleName"));
		role.setRoleDesc(WebUtils.getRequestParameterAsString(request, "roleDesc"));
		
		String rightValues [] = request.getParameterValues("rightValue");
		List<RightInfo> rightList= new ArrayList<RightInfo>();
		RightInfo ri = null ;
		if(rightValues != null && rightValues.length > 0){
			for(String right : rightValues){
				ri = new RightInfo();
				ri.setRightValue(right);
				rightList.add(ri);
			}
		}
		if(userDao.createRole(role, rightList)){
			message = "系统角色创建成功!" ;
			UserInfo u = (UserInfo) request.getSession().getAttribute(Constants.SESSIONNAME);
			logDao.create(u.getId(), LogConstants.ACTION_INSERT, NavigateConstants.NAVI_USER, "创建用户角色" + role.getRoleName());
		}else{
			message = "系统角色创建失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/user/role-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//系统用户--角色--修改
	@RequestMapping(value="/role-update.do")
	public ModelAndView roleUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		int roleId = WebUtils.getRequestParameterAsInt(request, "roleId",0);
		RoleInfo role = new RoleInfo();
		if(roleId > 0){
			role = userDao.getRoleInfoByRoleId(roleId);
			role.setRoleName(WebUtils.getRequestParameterAsString(request, "roleName"));
			role.setRoleDesc(WebUtils.getRequestParameterAsString(request, "roleDesc"));
			
			String rightValues [] = request.getParameterValues("rightValue");
			List<RightInfo> rightList= new ArrayList<RightInfo>();
			RightInfo ri = null ;
			if(rightValues != null && rightValues.length > 0){
				for(String right : rightValues){
					ri = new RightInfo();
					ri.setRightValue(right);
					rightList.add(ri);
				}
			}
			if(userDao.updateRole(role, rightList)){
				message = "系统角色修改成功!" ;
				UserInfo u = (UserInfo) request.getSession().getAttribute(Constants.SESSIONNAME);
				logDao.create(u.getId(), LogConstants.ACTION_INSERT, NavigateConstants.NAVI_USER, "修改用户角色" + role.getRoleName());
			}else{
				message = "系统角色修改失败!" ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/user/role-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
}